<template>
  <div class="tool_mbti">
    <TopHeader></TopHeader>
    <div class="mainTitle">
      <div class="container headArea">
        <div class="left">
          <div class="title">MBTI性格测试</div>
          <div class="describe">当前测试进度 {{ tabNum }} / 6</div>
        </div>
      </div>
    </div>
    <div :class="['container', 'qusArea', notAnsQus ? 'paddingTop24' : '']">
      <div class="notAnsQuestion" v-show="notAnsQus">
        <div class="tip">有题目尚未选择答案</div>
        <img class="closeImage" @click="notAnsQus = false" src="../../assets/icon_close_red.png" alt="">
      </div>
      <div v-for="(item,index) in mbti" :key="index">
        <div :class="['textQuestions', notAnsQus && hasChoIndexRadio[index] == undefined ? 'noFinishTheQus' : '']" v-show="item.isShow===tabNum">
          <div class="qusTitle">{{item.issue}}</div>
          <div class="choRadioArea">
            <div class="choItem" @click="handleChoSingleTextQus(index, item.answer1)">
              <img v-if="hasChoIndexRadio[index] > 0" src="../../assets/login/radio_checked.png" alt="">
              <img v-else src="../../assets/login/radio_unchecked.png" alt="">
              <span>{{ item.answer1.ans }}</span>
            </div>
            <div class="choItem" @click="handleChoSingleTextQus(index, item.answer2)">
              <img v-if="hasChoIndexRadio[index] == 0" src="../../assets/login/radio_checked.png" alt="">
              <img v-else src="../../assets/login/radio_unchecked.png" alt="">
              <span>{{ item.answer2.ans }}</span>
            </div>
          </div>
        </div>

        
      </div>
      <div class="submitBtn" v-if="tabNum < 6" @click="handleJumpNextGroup">下一组</div>
      <div class="submitBtn" v-else @click="handleCreateResult">提交生成结果</div>
    </div>
  </div>
</template>

<script>
import TopHeader from "~/components/topHeader";
import mbti from '~/static/json/mbti.json'
import ls from 'store2'
export default {
  components: {
    TopHeader,
  },
  data() {
    return {
      tabNum: 1,
      mbti: mbti.history,
      notAnsQus: false, // 是否完成了该组测试    true --> 未完成   false --> 已完成
      checkList: [],
      hasChoIndexRadio: [],
      hasChoAnswer: [], // 已选择的答案
      scrollTop: 0,
      mbtiScore: [
        {
					typr: "E",
					count: 0
				}, {
					typr: "I",
					count: 0
				}, {
					typr: "S",
					count: 0
				}, {
					typr: "N",
					count: 0
				}, {
					typr: "J",
					count: 0
				}, {
					typr: "P",
					count: 0
				}, {
					typr: "T",
					count: 0
				}, {
					typr: "F",
					count: 0
        }
      ],
    }
  },
  mounted() {
  },
  methods: {
    handleChoSingleTextQus(index, data) {
      // vue更新视图
      this.$set(this.hasChoIndexRadio, index, data.score)
      this.$set(this.hasChoAnswer, index, data.type)
    },
    // 下一组
    handleJumpNextGroup() {
      this.handleJudgeIsFinish()
      if (!this.notAnsQus) {
        // 如果完成了当前页面的测试，进入下一组
        this.$nextTick(() => {
          const that = this
          let timer = setInterval(() => {
            let ispeed = Math.floor(-that.scrollTop / 5)
            document.documentElement.scrollTop = document.body.scrollTop = that.scrollTop + ispeed
            if (that.scrollTop === 0) {
              clearInterval(timer)
            }
          }, 16)
				})
        this.tabNum += 1
      }
    },
    // 提交生成结果
    handleCreateResult() {
      this.handleJudgeIsFinish()
      if (!this.notAnsQus) {
        // this.tabNum += 1
        for (const item of this.hasChoAnswer) {
          if (item == 'E') {
            this.mbtiScore[0].count += 1
          } else if (item == "I") {
            this.mbtiScore[1].count += 1
          } else if (item == "S") {
            this.mbtiScore[2].count += 1
          } else if (item == "N") {
            this.mbtiScore[3].count += 1
          } else if (item == "J") {
            this.mbtiScore[4].count += 1
          } else if (item == "P") {
            this.mbtiScore[5].count += 1
          } else if (item == "T") {
            this.mbtiScore[6].count += 1
          } else if (item == "F") {
            this.mbtiScore[7].count += 1
          }
        }

        let obj = {
          status: 4,
          worthScore: this.mbtiScore,
          allResultAnswer: this.hasChoAnswer
        }
        ls.set('resultPar', obj)
        ls.set('mbtiResult', obj)
        this.commonJs.gosto('../testResult')
      }

    },
    // 判断当前组是否有题目未完成
    handleJudgeIsFinish() {
      for (let i = (this.tabNum - 1) * 10; i < 10 * this.tabNum; i += 1) {
        if (this.hasChoIndexRadio[i] == undefined) {
          this.notAnsQus = true
          return;
        } else {
          this.notAnsQus = false
        }
      }
    },
    
  }

}
</script>

<style lang="less" scoped>
  @defaultColor: #1C2227;
  .tool_mbti {
    padding-bottom: 100px;
    .mainTitle {
      width: 100%;
      height: 120px;
      border: 2px solid rgba(204,204,204,0.3);
      padding-top: 33px;
      .headArea {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left {
          .title {
            color: @defaultColor;
            font-size: 22px;
            line-height: 29px;
            font-weight: bold;
            margin-bottom: 8px;
          }
          .describe {
            color: #9E9E9E;
            font-size: 16px;
            line-height: 21px;
          }
        }
      }
    }
    .qusArea {
      padding-top: 64px;
      .notAnsQuestion {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 16px;
        margin-bottom: 24px;
        background: #FFF7F7;
        width: 100%;
        height: 44px;
        .tip {
          color: #EB4E3F;
          font-size: 14px;
        }
        .closeImage {
          width: 24px;
          height: 24px;
          cursor: pointer;
        }
      }
      .textQuestions {
        margin-bottom: 16px;
        padding: 8px;
        border: 1px solid transparent;
        .qusTitle {
          color: @defaultColor;
          font-size: 16px;
          line-height: 21px;
          font-weight: bold;
          margin-bottom: 24px;
        }
        .choRadioArea {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          .choItem {
            width: 360px;
            height: 40px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            cursor: pointer;
            .img {
              width: 16px;
              height: 16px;
            }
            span {
              color: #343A40;
              font-size: 16px;
              margin-left: 16px;
            }
          }
          .choItem+.choItem {
            margin-left: 32px;
          }
          .choItem:hover {
            background: #F7F7FA;
          }
        }
      }
      .noFinishTheQus {
        border-color: #EB4E3F;
        .qusTitle {
          color: #EB4E3F;
        }
      }
    }
    .paddingTop24 {
      padding-top: 24px;
    }
    .submitBtn {
      margin: 150px auto 0;
      width: 150px;
      height: 50px;
      line-height: 50px;
      background: #FF8A00;
      border-radius: 2px;
      color: #fff;
      text-align: center;
      font-size: 16px;
      font-weight: bold;
      cursor: pointer;
    }
  }
</style>